package br.edu.fatec.tutorial2d;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class TutorialThread extends Thread {

	private Panel panel;
	private SurfaceHolder surfaceHolder;
	private boolean isRun;
	
	public TutorialThread(Panel p, SurfaceHolder sh) {
		panel = p;
		surfaceHolder = sh;
		isRun = false;
	}
	
	public void setRunning(boolean value) {
		isRun = value;
	}

	@Override
	public void run() {
		Canvas canvas;
		
		while(isRun) {
			canvas = null;
			try {
				canvas = surfaceHolder.lockCanvas(null);
				synchronized (surfaceHolder) {
					panel.updatePhysics();
					panel.onDraw(canvas);
				}
			} finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}

	public SurfaceHolder getSurfaceHolder() {
		return surfaceHolder;
	}
	
}
